import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/entity/article_list_entity.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/SignUtil.dart';
import '../base/Constant.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../http/Api.dart';
import '../http/HttpUtil.dart';
import 'ArticleDetail.dart';

class InformNews2Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return InformNews2View();
  }
}

class InformNews2View extends State<InformNews2Page> {
  List<HomeListDataData> articles = new List();
  EasyRefreshController _controller = new EasyRefreshController();
  int page = 1;
  String uid = "";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      uid = value;
      loadList();
    });
  }

  loadList() async {
    Map map = new Map();
    map['token'] = uid;
    map['p'] = page.toString();
    map['per'] = "10";
    map['sign_type'] = SignType.GET_ARTICLE_LIST;
    Response<String> response =
        await HttpUtil().post(Api.BaseUrl+"/index.php/wap/UserMessage/getRecord", data: getSign(map));
    print(response.data);
    Map userMap = json.decode(response.data);
    HomeListEntity homeServiceEntity = new HomeListEntity.fromJson(userMap);
    if (userMap['code'] == 1 && mounted) {
      if (page == 1) {
        articles.clear();
        _controller.resetLoadState();
        _controller.finishRefresh();
      } else {
        _controller.finishLoad(
            noMore: homeServiceEntity.data.datas.length == 0);
        if (homeServiceEntity.data.datas.length == 0) {
          Fluttertoast.showToast(msg: "已经加载全部");
        }
      }
      articles.addAll(homeServiceEntity.data.datas);
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "升级通知",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.5,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      body: new Container(
        color: divideColor,
        child: new EasyRefresh(
          enableControlFinishLoad: true,
          enableControlFinishRefresh: true,
          header: MaterialHeader(),
          footer: MaterialFooter(),
          controller: _controller,
          child: ListView.separated(
              itemBuilder: _renderRow,
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  color: divideColor,
                  height: 1,
                );
              },
              itemCount: articles.length),
          onRefresh: () async {
            page = 1;
            loadList();
          },
          onLoad: () async {
            page++;
            loadList();
          },
        ),
      ),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    return new GestureDetector(
      onTap: () {
        // Navigator.of(context).push(MaterialPageRoute(builder: (context) {
        //   return new ArticleDetailPage(
        //     article_id: articles[index].article_id,
        //   );
        // }));
      },
      child: new Container(
        color: Colors.white,
        padding: EdgeInsets.all(10.0),
        child: Row(
          children: <Widget>[
            // Container(
            //   width: DP_100,
            //   height: DP_60,
            //   margin: EdgeInsets.only(right: 10),
            //   child: articles[index].img == ""
            //       ? Image.asset(
            //           "images/app_icon.png",
            //           fit: BoxFit.cover,
            //         )
            //       : CachedNetworkImage(
            //           imageUrl: Api.ImgBaseUrl + articles[index].img,
            //           fit: BoxFit.cover,
            //         ),
            // ),
            Expanded(
                child: new Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Row(
                  children: <Widget>[
                    Expanded(
                      child: Text(
                        articles[index].content,
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE),
                            color: Colors.black),
                      ),
                    ),
                  ],
                ),
                // Container(
                //   padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
                //   child: Text(
                //     articles[index].description,
                //     style: TextStyle(
                //         fontSize:
                //             ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                //         color: Colors.black54),
                //     maxLines: 2,
                //     overflow: TextOverflow.ellipsis,
                //   ),
                // ),
                Text(
                  articles[index].create_time,
                  style: TextStyle(
                      fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                      color: Colors.black54),
                )
              ],
            ))
          ],
        ),
      ),
    );
  }
}
